package com.server.base.util;

import org.springframework.jms.core.MessageCreator;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by xiehuilin on 2015/9/9.
 * 创建 mq TextMessage
 */
public class MQTextMessageCreater implements MessageCreator {

    //构造器传入所需携带参数
    public MQTextMessageCreater(Map<String, Object> param) {
        this.param = param;
    }

    private Map<String, Object> param = new HashMap<String, Object>();

    @Override
    public Message createMessage(Session session) throws JMSException {

        TextMessage textMessage = session.createTextMessage();

        //取出所有参数 放入message中
        Iterator<String> iterator = param.keySet().iterator();
        while(iterator.hasNext()){

            String key = iterator.next();
            Object value = param.get(key);

            if(value instanceof String){

                textMessage.setStringProperty(key, (String)value);

            }else if(value instanceof Integer){

                textMessage.setIntProperty(key, (Integer) value);

            }else if(value instanceof Long){

                textMessage.setLongProperty(key, (Long)value);

            }else if(value instanceof Boolean){

                textMessage.setBooleanProperty(key, (Boolean)value);

            }else if(value instanceof Byte){

                textMessage.setByteProperty(key, (Byte)value);

            }else if(value instanceof Float){

                textMessage.setFloatProperty(key, (Float)value);

            }else{

                textMessage.setObjectProperty(key, value);

            }

        }

        return textMessage;
    }
}
